﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TiTi.tea.Api.Models;
using TiTi.tea.BLL;
using TiTi.tea.Model;

namespace TiTi.tea.Api.Controllers
{
    [RoutePrefix("api/Menu")]
    public class MenuController : ApiController
    {
        private MenuBLL bll = new MenuBLL();
        [Route("")]
        [HttpGet]
        public Result Get(string MenuName="", int PageIndex=1, int PageSize=10) 
        {
            int total = 0;
            var data= bll.Query(MenuName, out total,PageIndex,PageSize);
            return Result.Success(new {data,total});
        }

        /// <summary>
        /// 获取菜单树
        /// </summary>
        /// <returns></returns>
        [Route("GetAll")]
        [HttpGet]
        public Result GetAll()
        {
            return Result.Success(bll.GetAll());
        }

        [Route("Add")]
        [HttpPost]
        public Result Add(MenuInfo info)
        {
          if (bll.Add(info))
            {

                return Result.Success();
            }
            else
            {
                return Result.Error("添加失败");
            }
        }

        [Route("FuCha")]
        [HttpGet]
        public Result FuCha()
        {
            return Result.Success(bll.FuQuery()); 
        }

        [Route("Update")]
        [HttpPost]
        public Result Update(MenuInfo info) 
        {
            if (info.Id==0)
            {
                return Result.Error("id不能为空");
            }
            else
            {
               MenuInfo me=bll.GetOne(info.Id);
               me.MenuName = info.MenuName;
                me.MenuUrl = info.MenuUrl;
                me.MenuIcon = info.MenuIcon;
                me.PId = info.PId;
              
                
                bool num=bll.Update(me);
                if (num) 
                {
                    return Result.Success();
                }
                else
                {
                    return Result.Error("修改失败");
                }
            }

        }

        [Route("GetOne")]
        [HttpGet]
        public Result GetOne(int id) 
        {
            return Result.Success(bll.GetOne(id));
        }

        public Result Delete(int id)
        {
            MenuInfo menu = bll.GetOne(id);
            if (menu == null)
            {
                return Result.Error("没有该对象");
            }
            menu.IsDelete = 1;

            bool boo = bll.Update(menu);
            if (boo)
            {
                return Result.Success();
            }
            else
            {
                return Result.Error("失败");
            }
        }
    }
}
